<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\admin\controller;

use app\BaseController;
use app\common\facade\admin\AdminAdmin;
use think\exception\ValidateException;

class Admin extends BaseController
{


    /**
     * 管理员列表
     */
    public function adminList()
    {

        $param = $this->request->post([
            'name',
            'offset' => 0,
            'length' => 20
        ]);

        try {
            $adminList = AdminAdmin::adminList($param);
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($adminList);
    }

    /**
     * 角色【所有】
     */
    public function roleAll()
    {

        try {
            $data['roleAll'] = AdminAdmin::roleAll(['enable', '=', 2], ['id', 'name']);
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 权限【管理员、所有】
     */
    public function ruleAdminAll()
    {

        try {
            $data['rules'] = AdminAdmin::ruleAdminAll();
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson($data);
    }

    /**
     * 管理员添加【保存】
     */
    public function adminAddSave()
    {

        $param = $this->request->post(['name', 'password', 'password2', 'enable', 'roleId']);

        try {
            $this->validate($param, 'app\common\validate\Admin.addSave');
            AdminAdmin::adminAddEditSave($param);
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson();
    }

    /**
     * 管理员编辑【保存】
     */
    public function adminEditSave()
    {

        $param = $this->request->post(['id', 'password', 'password2', 'enable', 'roleId']);

        try {
            $this->validate($param, 'app\common\validate\Admin.editSave');
            AdminAdmin::adminAddEditSave($param);
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson();
    }

    /**
     * 修改密码【保存】
     */
    public function passwordEditSave()
    {

        $param = $this->request->post(['password', 'password2']);

        try {
            $this->validate($param, 'app\common\validate\Admin.passwordEditSave');
            AdminAdmin::passwordEditSave($param);
        } catch (ValidateException $e) {
            return validateJson($e->getError());
        } catch (\Exception $e) {
            return exceptionJson($e->getMessage(), $e->getCode());
        }

        return successJson([], '修改密码成功');
    }



}
